package hips.gui.images;

import ij.ImagePlus;
import ij.process.ColorProcessor;
import hips.images.rgb.PixelValueRGB;
import hips.images.rgb.ImageRGB;
import hips.region.Region;

public class Image4 extends hips.images.Image<PixelValueRGB, Integer>{
    int[] r, ir;

    public Image4(int width, int height, String title){
        this.height = height;
        this.width = width;
        size = height * width;
        slices = 4;
        ij.ImageStack stack = new ij.ImageStack(width, height);
        stack.addSlice("R G B", new ColorProcessor(width, height));
        stack.addSlice("IR G B", new ColorProcessor(width, height));
        impl = new ImagePlus(title, stack);
        r = (int[]) stack.getProcessor(1).getPixels();
        ir = (int[]) stack.getProcessor(2).getPixels();
    }



//    public PValueStack newPixelValue(int value){
//        PValueStack p = new PValueStack(4);
//        for (int i = 0; i < 4; i++){
//            p.slices[i] = new PixelValue(value, value, value);
//        }
//        return p;
//    }

    public PixelValueRGB newPixelValue(Integer value){
        PixelValueRGB p = new PixelValueRGB(4);
        p.setValueInt(ImageRGB.toRGB(ImageRGB.getRed(value), 0, 0), 0);
        p.setValueInt(ImageRGB.toRGB(ImageRGB.getGreen(value), 0, 0), 1);
        p.setValueInt(ImageRGB.toRGB(ImageRGB.getBlue(value), 0, 0), 2);
        p.setValueInt(ImageRGB.toRGB(ImageRGB.getRed(value), 0, 0), 3);

        return p;
    }

    public void putPixelValue(int index, PixelValueRGB p) {
        r[index] = ImageRGB.toRGB(
                ImageRGB.getRed(p.getValue(0)),
                ImageRGB.getRed(p.getValue(1)),
                ImageRGB.getRed(p.getValue(2)));
        ir[index] = ImageRGB.toRGB(
                ImageRGB.getRed(p.getValue(3)),
                ImageRGB.getRed(p.getValue(1)),
                ImageRGB.getRed(p.getValue(2)));
    }

//    public static String getStringFrom(hips.pixel.PixelValue p, int slice){
//        hips.images.gray8.PixelValue pixel = (hips.images.gray8.PixelValue) p;
//        if (slice == 0){
//            return  "R=" + pixel.getValueInt(0) +
//                    ",G=" + pixel.getValue(1) +
//                    ",B=" + pixel.getValueInt(2);
//        }
//        return  "IR=" + pixel.getValueInt(3) +
//                ",G=" + pixel.getValue(1) +
//                ",B=" + pixel.getValueInt(2);
//    }

    public static String getStringFrom(hips.pixel.PixelValue p, int slice){
        if (slice == 0){
            return  "R=" + p.getValue(0) +
                    ",G=" + p.getValue(1) +
                    ",B=" + p.getValue(2);
        }
        return  "IR=" + p.getValue(3) +
                ",G=" + p.getValue(1) +
                ",B=" + p.getValue(2);
    }

    public void colourComponent(Region c, PixelValueRGB color) {
        for (int i = 0; i < c.getSize(); i++) {
            putPixelValue(c.getPixelPosition(i), color);
        }
        impl.updateAndDraw();
    }

//    public void putPixel(int index, PixelValue p) {
//        r[index] = segimasat.image.rgb.Image.toRGB(p.slices[0], p.slices[1], p.slices[2]);
//        ir[index] = segimasat.image.rgb.Image.toRGB(p.slices[3], p.slices[1], p.slices[2]);
//    }

//    public void colourComponent(Component c, segimasat.image.Image colorSource) {
//        segimasat.pixel.PixelSum colorSum = colorSource.newPixelSum();
//        for (int i = 0; i < c.getSize(); i++) {
//            colorSum.add(colorSource.getPixel(c.getPixel(i)));
//        }
//        segimasat.pixel.PixelValue color = colorSum.newPixelValue(c.getSize());
//        for (int i = 0; i < c.getSize(); i++) {
//            putPixel(c.getPixel(i), (PValueStack) color.getRGB(colorSource.minValueVector, colorSource.maxValueVector)); // ESTE CAST HAY QUE KITARLO.
//        }
//        impl.updateAndDraw();
//    }

    public Integer getZero() {
        return 0;
    }

    @Override
    public PixelValueRGB getPixelValue(int index) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public hips.images.Image newImage() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public ImageRGB newImageRGB() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public PixelValueRGB newPixelValue() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public PixelValueRGB toRGB(PixelValueRGB pv) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public PixelValueRGB getMeanValue(Region region) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
